package com.xxq.aidlclient.Activity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class JsoupParse {
    public final String mNewsURL = "https://news.sina.com.cn/world/";

    public static void main(String[] args) {

        System.out.println("开始解析网页");

        try {
            Document doc = Jsoup.connect("https://news.sina.com.cn/world/").userAgent("Mozilla").timeout(3000).get();

            Elements elements = doc.select(".news-item");

            if (!elements.isEmpty()) {
                for (Element element : elements) {
                    Elements h2Tags = element.getElementsByTag("h2");
                    if (h2Tags.size() > 0) {

                        Element time = element.select(".time").first();
                        String newsTime = time.text();

                        Element a = element.select("a[href]").first();
                        String newsTitle = a.text();
                        String newsUrl = a.attr("href");

                        System.out.println(newsTime + " " + newsTitle + " " + newsUrl);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
        }

    }

    public void getLatestNewsList(String url) {
        System.out.println("开始解析网页");
        try {
            Document doc = Jsoup.connect(url).userAgent("Mozilla").timeout(3000).get();

            Elements elements = doc.select(".news-item");

            if (!elements.isEmpty()) {
                for (Element element : elements) {
                    Elements h2Tags = element.getElementsByTag("h2");
                    if (h2Tags.size() > 0) {

                        Element time = element.select(".time").first();
                        String newsTime = time.text();

                        Element a = element.select("a[href]").first();
                        String newsTitle = a.text();
                        String newsUrl = a.attr("href");

                        System.out.println(newsTime + " " + newsTitle + " " + newsUrl);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
        }
    }
}
